package com.qianfeng.zhangwei.xytg.http;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;

/**
 * @说 明：
 * @项目名称：Sz1504
 * @包 名： com.qianfeng.zhangwei.day26.utils
 * @类 名： HttpUtils
 * @创 建人： zhangwei
 * @创建时间：2015-07-20 16:13
 * @版 本：v1.0
 * @修 改人：
 * @修改时间：
 * @修改备注：
 */
public class HttpUtils {
    public static final int DEFAULT_READ_TIME_OUT = 6 * 1000;
    public final int readTimeOut;

    public HttpUtils() {
        this(DEFAULT_READ_TIME_OUT);
    }
    public HttpUtils(int readTimeOut) {
        this.readTimeOut = readTimeOut;
    }

    /**
     * @param url
     * @return
     */
    public String send(HttpMethod method, String url) {
        String result = null;
        return result;
    }

    private String httpPost(HttpMethod method, String url, Map<String, String> params) {
        String result = null;
        InputStream inputStream = null;
        BufferedReader bufferedReader = null;
        try {
            HttpURLConnection connection = getUrlConnection(method, url);
            connection.connect();
            if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;

    }

    private String getRequestParams(Map<String, String> params) {
        StringBuffer requestParams = new StringBuffer();
        for (Map.Entry<String, String> entry : params.entrySet()) {
            requestParams.append(entry.getKey());
            requestParams.append("=");
            requestParams.append(entry.getValue());
            requestParams.append("&");
        }
        if (requestParams.length() > 0) {
            requestParams.deleteCharAt(requestParams.length() - 1);
        }
        return requestParams.toString();
    }

    private HttpURLConnection getUrlConnection(HttpMethod method, String requestUrl) throws IOException {
        URL url = new URL(requestUrl);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        switch (method) {
            case POST:
                connection.setDoInput(true);
                connection.setDoOutput(true);
                connection.setRequestMethod("POST");
                connection.setReadTimeout(DEFAULT_READ_TIME_OUT);
                connection.setUseCaches(false);
                break;
            case GET:
                connection.setRequestMethod("GET");
                connection.setReadTimeout(DEFAULT_READ_TIME_OUT);
                connection.setUseCaches(false);
                break;

        }
        return connection;
    }


    public interface OnCallbackListener {
        public void onSucceedListener(String result);

    }


}
